package n1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 获取线程结果
 */
@Slf4j(topic = "c.T2_FutureTask")
public class T2_FutureTask {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        test1();
    }

    public static void test1() throws ExecutionException, InterruptedException {

        // 1. 新建可传递参数的 任务线程
        final FutureTask<Integer> task = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws InterruptedException {
                log.debug("FutureTask use Callable get 10086!");
                Thread.sleep(3000);
                if(true)throw new RuntimeException("报错报错报错！！！！！");
                return 10086;
            }
        });

        // 2. 执行
        new Thread(task, "T1").start();

        // 3. 获取结果  【-【-【------ 这里get() 会阻塞等待 ------】-】-】
        Integer i = task.get();
//        Integer i = 111;


        System.out.println("最终获得整数：" + i);
        log.debug("最终获得 = {} " , i);

    }


}
